import { BaseAvatar } from "./BaseAvatar";
import { SPublicMiniPlayer } from "../../../../Message/Public/SPublicMiniPlayer";
import { ThingUtil } from "../../scene/layer/utils/ThingUtil";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { NetDispatcher } from "../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../mvc/ServerCommand";
import { DataEvent } from "../../events/DataEvent";
import { SEntityInfo } from "../../../../Message/BroadCast/SEntityInfo";
import { Cache } from "../../cache/Cache";
import { SEntityId } from "../../../../Message/Public/SEntityId";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { GameMapUtil } from "../../scene/map/GameMapUtil";
import { EntityUtil } from "../../scene/layer/utils/EntityUtil";
import { PlayerOpRegister } from "../common/menu/PlayerOpRegister";
import { RolePlayer } from "../../scene/player/entity/RolePlayer";
import { PlayerOpMenuConst } from "../common/menu/PlayerOpMenuConst";
import { PlayerEvent } from "../../scene/events/PlayerEvent";
import { TeamTalkSprite } from "../../scene/player/TeamTalkSprite";
import { FaceAuthority } from "../chat/data/FaceAuthority";
import { EntityType } from "../../rules/EntityType";
import { ObjCreate } from "../common/ObjCreate";
import { GUIComponent } from "../../../../com/mui/controls/GUIComponent";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { Language } from "../../../../extend/language/Language";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { PlayerMiniInfo } from "../../model/PlayerMiniInfo";
import { AvatarInfo } from "../../resource/info/AvatarInfo";
import { AvatarType } from "../../resource/info/AvatarType";
import { IEntity } from "../../scene/player/IEntity";
import { GButton } from "../../../../com/mui/controls/GButton";
import { TalkSprite } from "../../scene/player/TalkSprite";
type int = number;
//class TeammateAvatar
    
    export  class TeammateAvatar extends BaseAvatar
    {
        constructor(arg1: SPublicMiniPlayer)
        {
            this._publicMiniPlayer = arg1;
            if (arg1) 
            {
                this.selfSEntityId = arg1.entityId;
                this._iEntity = ThingUtil.entityUtil.getEntity(this.selfSEntityId);
                if (this._iEntity) 
                {
                    this._entityInfo = this._iEntity.entityInfo.entityInfo;
                    this.addListner(this.selfSEntityId);
                }
                else 
                {
                    this._entityInfo = this.getDefaultEntityInfo();
                    this._entityInfo.entityId = this.selfSEntityId;
                    this._entityInfo.name = arg1.name;
                    this._entityInfo.level = arg1.level;
                    this._entityInfo.camp = arg1.camp;
                    this._entityInfo.sex = arg1.sex;
                }
            }
            else 
            {
                this._entityInfo = this.getDefaultEntityInfo();
            }
            super(this._entityInfo);
            if (arg1 == null) 
            {
                hide();
            }
            this.updateByPublicMiniPlayer(arg1);
            Dispatcher.addEventListener(EventName.Group_StepIn, this.onGroupStepInHandler);
            Dispatcher.addEventListener(EventName.Group_StepOut, this.onGroupStepOutHandler);
            Dispatcher.addEventListener(EventName.ChangeScene, this.onGroupStepOutHandler);
            NetDispatcher.addCmdListener(ServerCommand.Group_CaptainChange, this.onGroupCaptainChange);
            NetDispatcher.addCmdListener(ServerCommand.CrossGroup_CaptainChange, this.onGroupCaptainChange);
            return;
        }

        /* internal  */onGroupStepInHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityInfo;
            if (loc1) 
            {
                if (this.isSelf(loc1.entityId)) 
                {
                    this.updateAvatarUI(loc1);
                    this._status = 1;
                    this.enableAvatar();
                    this.addListner(loc1.entityId);
                }
            }
            return;
        }

        /* internal  */onSceneChangeHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=Cache.instance.group.players;
            for(loc1 of loc3) 
            {
                this.updateByPublicMiniPlayer(loc1);
            }
            return;
        }

        /* internal  */onGroupStepOutHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            if (loc1 == null) 
            {
                if (this._status != 0) 
                {
                    this._status = 2;
                }
                this.disableAvatar();
            }
            if (this.isSelf(loc1)) 
            {
                if (!Cache.instance.marry.isSelfAtCeremonyOrParade()) 
                {
                    this._status = 2;
                    this.disableAvatar();
                    this.removeListner();
                }
            }
            return;
        }

        /* internal  */disableAvatar(): void
        {
            avatarBitmap.filters = [FilterConst.colorFilter];
            if (this._status != 0) 
            {
                if (this._status == 2) 
                {
                    this._statusBitmap.bitmapData = GlobalClass.getBitmapData(ImagesConst.TeammateFar);
                }
            }
            else 
            {
                this._statusBitmap.bitmapData = GlobalClass.getBitmapData(ImagesConst.TeammateDownline);
            }
            this._statusBitmap.visible = true;
            return;
        }

        /* internal  */setTeamMark(): void
        {
            var loc2=null;
            if (this._teamMarkBitmap == null) 
            {
                this._teamMarkBitmap = new Object /* flash.display.Bitmap */();
            }
            var loc1=GameMapUtil.isCrossGroupMap();
            if (GameMapUtil.curMapState.isGuildElite) 
            {
                loc2 = Cache.instance.guildElite.captain;
            }
            else if (loc1) 
            {
                loc2 = Cache.instance.crossGroup.captain;
            }
            else 
            {
                loc2 = Cache.instance.group.captain;
            }
            if (loc2 && this.selfSEntityId) 
            {
                if (EntityUtil.equal(loc2, this.selfSEntityId)) 
                {
                    this._teamMarkBitmap.bitmapData = GlobalClass.getBitmapData(ImagesConst.TeamLeaderS);
                    if (this.contains(this._teamMarkBitmap) == false) 
                    {
                        this.addChild(this._teamMarkBitmap);
                    }
                }
                else 
                {
                    this._teamMarkBitmap.bitmapData = null;
                    if (this.contains(this._teamMarkBitmap) == false) 
                    {
                        this.addChild(this._teamMarkBitmap);
                    }
                }
            }
            else 
            {
                this._teamMarkBitmap.bitmapData = null;
                if (this.contains(this._teamMarkBitmap) == false) 
                {
                    this.addChild(this._teamMarkBitmap);
                }
            }
            return;
        }

        /* internal  */enableAvatar(): void
        {
            avatarBitmap.filters = [];
            this._statusBitmap.visible = false;
            return;
        }

        /* internal  */onGroupCaptainChange(arg1: Object): void
        {
            this.setTeamMark();
            this.reRegistOperMenu();
            return;
        }

        /* internal  */reRegistOperMenu(): void
        {
            var loc2=null;
            PlayerOpRegister.UnRegister(this._optionBtn);
            var loc1=GameMapUtil.isCrossGroupMap();
            if (loc1) 
            {
                loc2 = Cache.instance.crossGroup.captain;
            }
            else 
            {
                loc2 = Cache.instance.group.captain;
            }
            if (loc2) 
            {
                if (EntityUtil.equal(loc2, RolePlayer.instance.entityInfo.entityInfo.entityId)) 
                {
                    PlayerOpRegister.Register(this._optionBtn, this._playerMiniInfo, PlayerOpMenuConst.GroupCaptainOpMenu);
                }
                else 
                {
                    PlayerOpRegister.Register(this._optionBtn, this._playerMiniInfo, PlayerOpMenuConst.GroupMemOpMenu);
                }
            }
            return;
        }

        /* internal  */addListner(arg1: SEntityId): void
        {
            this._iEntity = ThingUtil.entityUtil.getEntity(arg1);
            if (this._iEntity) 
            {
                this._iEntity.addEventListener(PlayerEvent.UPDATEINFO, this.onPlayerInfoUpdate);
            }
            return;
        }

        /* internal  */removeListner(): void
        {
            if (this._iEntity) 
            {
                this._iEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onPlayerInfoUpdate);
                this._iEntity = null;
                avatarBg.filters = [];
            }
            return;
        }

        /* internal  */onOperSpriteClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=ThingUtil.entityUtil.getEntity(this._entityInfo.entityId);
            if (loc1) 
            {
                loc1.selected = true;
            }
            return;
        }

        public talk(arg1: string, arg2: string=""): void
        {
            if (!(arg1 == null) && !(arg1 == "")) 
            {
                if (this._talkText == null) 
                {
                    this._talkText = new TeamTalkSprite();
                    this.addChild(this._talkText);
                }
                if (this._timerMask > 0) 
                {
                    flash.utils.clearTimeout(this._timerMask);
                    this._timerMask = 0;
                }
                this._timerMask = flash.utils.setTimeout(this.talk, 5000, null);
                this._talkText.x = 150;
                this._talkText.y = 10;
                if (this._talkText.parent != this) 
                {
                    this.addChild(this._talkText);
                }
                if (arg2 == "" || arg2 == "0") 
                {
                    this._talkText.modelId = "";
                }
                else 
                {
                    this._talkText.modelId = arg2 + "_1";
                }
                this._talkText.faceAuthortiy = FaceAuthority.getVIPAuthority(this._entityInfo.VIP);
                this._talkText.text = arg1;
            }
            else 
            {
                if (this._talkText && this._talkText.parent == this) 
                {
                    this.removeChild(this._talkText);
                }
                if (this._timerMask > 0) 
                {
                    flash.utils.clearTimeout(this._timerMask);
                    this._timerMask = 0;
                }
            }
            return;
        }

        /* internal  */getDefaultEntityInfo(): SEntityInfo
        {
            if (this._defaultEntityInfo == null) 
            {
                this._defaultEntityInfo = new SEntityInfo();
                this._defaultEntityInfo.entityId = new SEntityId();
                this._defaultEntityInfo.entityId.type = EntityType.Player;
                this._defaultEntityInfo.name = "";
            }
            return this._defaultEntityInfo;
        }

        public /* override */ init(): void
        {
            this.isShowHealthLabelTip = false;
            this.isShowManaLabelTip = false;
            super.init();
            createEffects(5, 73, 40);
            this._statusBitmap = GlobalClass.getBitmap(ImagesConst.TeammateDownline);
            this._statusBitmap.width = 45;
            this._statusBitmap.height = 23;
            this._statusBitmap.x = 13;
            this._statusBitmap.y = 15;
            this.addChild(this._statusBitmap);
            this._teamMarkBitmap = new Object /* flash.display.Bitmap */();
            this._teamMarkBitmap.x = 140;
            this._teamMarkBitmap.y = 5;
            this.addChild(this._teamMarkBitmap);
            this._optionBtn = ObjCreate.createGButton("", 15, 45, 41, 18, this, "OptionButton");
            this._operSprite = new GUIComponent();
            this._operSprite.graphics.beginFill(16711680, 0);
            this._operSprite.graphics.drawCircle(34, 25, 20);
            this._operSprite.graphics.endFill();
            this._operSprite.buttonMode = true;
            this._operSprite.cacheAsBitmap = true;
            this._operSprite.toolTipData = HTMLUtil.addColor(Language.getString(50061), "#ffffff");
            this.addChild(this._operSprite);
            this._operSprite.addEventListener(flash.events.MouseEvent.CLICK, this.onOperSpriteClickHandler);
            this.resetPosition();
            this.resetAvatar();
            return;
        }

        protected /* override */ onAvatarLoaded(arg1: ImageInfo): void
        {
            avatarBitmap.bitmapData = arg1.bitmapData;
            avatarBitmap.width = 42;
            avatarBitmap.height = 42;
            return;
        }

        public resetAvatar(): void
        {
            mainBgName = ImagesConst.PetBg;
            healthBar.showPercentText = true;
            magicBar.showPercentText = true;
            healthBar.setSize(79, 8);
            healthBar.progressBarClass = ImagesConst.Shengmingtiao;
            magicBar.setSize(79, 8);
            magicBar.progressBarClass = ImagesConst.Mofatiao;
            this.resetPosition();
            this.updateAvatarUI(this._entityInfo);
            return;
        }

        /* internal  */updateOperMenu(arg1: SEntityInfo): void
        {
            if (arg1 && arg1.entityId) 
            {
                if (arg1.entityId.type != EntityType.Player) 
                {
                    PlayerOpRegister.UnRegister(this._optionBtn);
                }
                else 
                {
                    if (this._sMiniPlayer == null) 
                    {
                        this._sMiniPlayer = new SPublicMiniPlayer();
                    }
                    this._sMiniPlayer.entityId = arg1.entityId;
                    this._sMiniPlayer.camp = arg1.camp;
                    this._sMiniPlayer.career = arg1.career;
                    this._sMiniPlayer.careerEnable = arg1.careerEnable;
                    this._sMiniPlayer.name = arg1.name;
                    this._sMiniPlayer.online = 1;
                    this._sMiniPlayer.sex = arg1.sex;
                    this._playerMiniInfo = new PlayerMiniInfo(this._sMiniPlayer);
                    this.reRegistOperMenu();
                }
            }
            return;
        }

        public /* override */ updateAvatarUI(arg1: SEntityInfo): void
        {
            var loc1=0;
            this.entityInfo = arg1;
            this.selfSEntityId = arg1.entityId;
            super.updateAvatarUI(arg1);
            if (this._publicMiniPlayer && this._publicMiniPlayer.avatarId > 0) 
            {
                loc1 = this._publicMiniPlayer.avatarId;
            }
            else 
            {
                loc1 = arg1.avatarId;
            }
            avatarName = AvatarInfo.getPlayerAvatarName(arg1.camp, arg1.sex, AvatarType.SMALL, loc1);
            this.updateOperMenu(arg1);
            this.setTeamMark();
            this.updateFightingHighlight(arg1.fighting);
            return;
        }

        /* internal  */updateFightingHighlight(arg1: int): void
        {
            if (arg1 != 1) 
            {
                avatarBg.filters = [];
            }
            else 
            {
                avatarBg.filters = [FilterConst.fightAvatarFilter];
            }
            return;
        }

        public updateByPublicMiniPlayer(arg1: SPublicMiniPlayer): void
        {
            this._publicMiniPlayer = arg1;
            if (arg1) 
            {
                this.selfSEntityId = arg1.entityId;
                this.setTeamMark();
                if (arg1.online != 0) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Group_Online, arg1.entityId));
                    this._iEntity = ThingUtil.entityUtil.getEntity(arg1.entityId);
                    if (this._iEntity) 
                    {
                        this._entityInfo = this._iEntity.entityInfo.entityInfo;
                        this.updateAvatarUI(this._entityInfo);
                        this.addListner(arg1.entityId);
                        this._status = 1;
                        this.enableAvatar();
                    }
                    else 
                    {
                        this.updateByNotIEntity(arg1);
                        this._status = 2;
                        this.disableAvatar();
                    }
                }
                else 
                {
                    this.updateByNotIEntity(arg1);
                    this._status = 0;
                    this.disableAvatar();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Group_Downline, arg1.entityId));
                }
                this.visible = true;
            }
            else 
            {
                this.visible = false;
            }
            return;
        }

        /* internal  */updateByNotIEntity(arg1: SPublicMiniPlayer): void
        {
            this._entityInfo = this.getDefaultEntityInfo();
            this._entityInfo.entityId = arg1.entityId;
            this._entityInfo.name = arg1.name;
            this._entityInfo.level = arg1.level;
            this._entityInfo.camp = arg1.camp;
            this._entityInfo.sex = arg1.sex;
            this._entityInfo.life = 0;
            this._entityInfo.maxLife = 0;
            this._entityInfo.mana = 0;
            this._entityInfo.maxMana = 0;
            this.updateAvatarUI(this._entityInfo);
            return;
        }

        /* internal  */onPlayerInfoUpdate(arg1: PlayerEvent): void
        {
            var loc1=arg1.player;
            if (loc1 && this.isSelf(loc1.entityInfo.entityInfo.entityId)) 
            {
                this.updateAvatarUI(loc1.entityInfo.entityInfo);
            }
            return;
        }

        public resetPosition(): void
        {
            levelLabel.x = 11;
            levelLabel.y = 35;
            nameLabel.x = 50;
            nameLabel.y = 0;
            healthBar.move(60, 16);
            magicBar.move(60, 25);
            avatarBitmap.x = 13;
            avatarBitmap.y = 6;
            return;
        }

        /* internal  */isSelf(arg1: SEntityId): Boolean
        {
            var loc1=false;
            if (this.selfSEntityId && arg1) 
            {
                if (EntityUtil.toString(this.selfSEntityId) == EntityUtil.toString(arg1)) 
                {
                    this.selfSEntityId = arg1;
                    loc1 = true;
                }
            }
            return loc1;
        }

        public /* var */selfSEntityId: SEntityId;

        /* internal */ /* var */_sMiniPlayer: SPublicMiniPlayer;

        /* internal */ /* var */_iEntity: IEntity;

        /* internal */ /* var */_defaultEntityInfo: SEntityInfo;

        /* internal */ /* var */_entityInfo: SEntityInfo;

        /* internal */ /* var */_statusBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_teamMarkBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_playerMiniInfo: PlayerMiniInfo;

        /* internal */ /* var */_optionBtn: GButton;

        /* internal */ /* var */_operSprite: GUIComponent;

        /* internal */ /* var */_status: int=0;

        /* internal */ /* var */_publicMiniPlayer: SPublicMiniPlayer;

        /* internal */ /* var */_talkText: TalkSprite;

        /* internal */ /* var */_timerMask: uint=0;
    }
